package com.tourex.ui.view.app;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.tourex.App;
import com.tourex.C;
import com.tourex.R;
import com.tourex.ui.GeneralListActivity;
import com.tourex.ui.MainActivity2;
import com.tourex.ui.SecondActivity;
import com.tourex.utils.BaseUtils;
import com.tourex.utils.LogUtils;

public class MainModuleLayout extends LinearLayout {

	public MainModuleLayout(Context context) {
		this(context, null);
	}

	public MainModuleLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater.from(getContext()).inflate(R.layout.layout_module, this);
		findViewById(R.id.img_module1).setOnClickListener(moduleListener);
		findViewById(R.id.img_module2).setOnClickListener(moduleListener);
		findViewById(R.id.img_module3).setOnClickListener(moduleListener);
		findViewById(R.id.img_module4).setOnClickListener(moduleListener);

		this.post(new Runnable() {
			@Override
			public void run() {
				LinearLayout.LayoutParams LP = (LinearLayout.LayoutParams) MainModuleLayout.this.getLayoutParams();
				int margins = BaseUtils.dip(getContext(), 10);
				LP.setMargins(0, margins, 0, 0);
				MainModuleLayout.this.setLayoutParams(LP);
			}
		});
	}

	private OnClickListener moduleListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			int which = 0;
			switch (view.getId()) {
			case R.id.img_module1:
				which = 0;
				break;
			case R.id.img_module2:
				which = 1;
				break;
			case R.id.img_module3:
				which = 2;
				break;
			case R.id.img_module4:
				which = 3;
				break;
			}
			if (getContext() instanceof MainActivity2) {
				if (App.i().hasLocation) {
					if (1 == which) {
						GeneralListActivity.actionStart(getContext(), C.convention.type_njl, null);
					} else {
						SecondActivity.actionStart(getContext(), which, App.i().mli.cityCode);
					}
				} else {
					Toast.makeText(getContext(), R.string.location_3, Toast.LENGTH_SHORT).show();
				}
			} else {
				LogUtils.loge("MainModuleLayout not use in MainActivity");
			}
		}
	};

	public void setData() {

	}
}
